﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace SuperFind
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static bool ShutdownApplication = false;
        public static System.Windows.Forms.NotifyIcon SystemTrayIcon;
        public static System.Windows.Forms.ContextMenuStrip NotificationMenuStrip;
        public App()
        {
            SystemTrayIcon = new System.Windows.Forms.NotifyIcon();
            SystemTrayIcon.Icon = new System.Drawing.Icon(@"images\Application.ico");
            Dispatcher.ShutdownStarted+=new EventHandler(Dispatcher_ShutdownStarted);
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            SystemTrayIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(SystemTrayIcon_MouseDoubleClick);
            SystemTrayIcon.Visible = true;
        }
        void SystemTrayIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            App.Current.MainWindow.Show();
        }
        void  Dispatcher_ShutdownStarted(object sender, EventArgs e)
        {
            if (NotificationMenuStrip != null)
            {
                SystemTrayIcon.Dispose();
            }
 	            
        }
        public static void ShowNotification(string tipText, string tipTitle = "",int timeout=0)
        {
            SystemTrayIcon.BalloonTipText = tipText;
            SystemTrayIcon.BalloonTipTitle = tipTitle;
            SystemTrayIcon.ShowBalloonTip(timeout);
        }
        private void CreateSystemtrayContextMenu()
        {
            if (SystemTrayIcon == null)
            {
                SystemTrayIcon = new System.Windows.Forms.NotifyIcon();
            }
            if (NotificationMenuStrip == null)
            {
                NotificationMenuStrip = new System.Windows.Forms.ContextMenuStrip();
            }
            SystemTrayIcon.ContextMenuStrip = NotificationMenuStrip;
            System.Windows.Forms.ToolStripMenuItem mnu = new System.Windows.Forms.ToolStripMenuItem("E&xit");
            mnu.Click+=new EventHandler(mnu_Click);
            NotificationMenuStrip.Items.Add(mnu);
            SystemTrayIcon.ContextMenuStrip = NotificationMenuStrip;
        }
        void  mnu_Click(object sender, EventArgs e)
        {
            Application.Current.Shutdown(0);
        }
        public static void CloseApplication(bool shutdown)
        {
            ShutdownApplication = shutdown;
            App.Current.MainWindow.Close();
        }
    }
}
